package com.bk.game.screen;

import java.util.Collections;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.bk.game.global.BkGlobal;
import com.bk.game.mc.TopBtn;

/**
 * 描述:结束
 * 
 * @author linqiang(866116)
 * @Since 2012-9-5
 */
public class EndScreen implements Screen, InputProcessor {

	// 舞台
	private Stage stage;

	private TextureAtlas bgta;

	private Label label;

	private BitmapFont bf;

	private int score;

	private TextureRegion region1 = null;

	private Sound sound;

	@Override
	public void dispose() {
		if (sound != null) {
			sound.dispose();
		}
		bf.dispose();
		bgta.dispose();
		stage.dispose();
	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
		if (region1 != null) {
			stage.getSpriteBatch().begin();
			stage.getSpriteBatch().draw(region1, 0, 700 * BkGlobal.factory, region1.getRegionWidth() * BkGlobal.factorx, region1.getRegionHeight() * BkGlobal.factorx);
			stage.getSpriteBatch().end();
		}
	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {
	}

	@Override
	public void show() {
		Gdx.input.setCatchBackKey(true);
		// 初始化舞台
		stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
		bf = new BitmapFont(Gdx.files.internal("font.fnt"), Gdx.files.internal("font.png"), false);
		bgta = BkGlobal.manager.get("end.pack", TextureAtlas.class);
		TextureRegion region = bgta.findRegion("bg_end");
		Image bg = new Image(region);
		float scalex = (float) Gdx.graphics.getWidth() / region.getRegionWidth();
		float scaley = (float) Gdx.graphics.getHeight() / region.getRegionHeight();
		bg.scaleX = scalex;
		bg.scaleY = scaley;
		stage.addActor(bg);

		score = BkGlobal.score;
		label = new Label(score + "", new Label.LabelStyle(bf, Color.BLACK));
		bf.setScale(1.5f * BkGlobal.factory);
		label.x = (410 - label.getPrefWidth()) * BkGlobal.factorx;
		label.y = 570 * BkGlobal.factory - label.getPrefHeight() / 2;
		stage.addActor(label);

		List<Integer> list = BkGlobal.getInstance().ranks;
		list.add(score);
		Collections.sort(list);
		Collections.reverse(list);

		// 没有进入纪录榜
		if (list.get(5) == score) {
			// region1 = bgta.findRegion("hiscore");
		} else {
			region1 = bgta.findRegion("hiscore1");
			sound = BkGlobal.manager.get("kekka.mp3", Sound.class);
			sound.play();
		}
		list.remove(5);
		BkGlobal.getInstance().rank();

		// 按钮
		TopBtn btn_end2 = new TopBtn("btn_end2", bgta.findRegion("btn_end2"), 376, 235, 220, 100);
		stage.addActor(btn_end2);

		TopBtn btn_end1 = new TopBtn("btn_end1", bgta.findRegion("btn_end1"), 376, 106, 220, 100);
		stage.addActor(btn_end1);

		TopBtn twitter = new TopBtn("twitter", bgta.findRegion("twitter"), 69, 106, 282, 100);
		stage.addActor(twitter);

		BkGlobal.flyActors.clear();
		BkGlobal.actors.clear();

		InputMultiplexer multiplexer = new InputMultiplexer();
		multiplexer.addProcessor(this);
		multiplexer.addProcessor(stage);
		Gdx.input.setInputProcessor(multiplexer);
	}

	@Override
	public boolean keyDown(int arg0) {
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		if (arg0 == Input.Keys.BACK) {
			BkGlobal.currentGame.setScreen(BkGlobal.mainScreen);
		}
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}
}
